package com.cc.service.impl;

import com.cc.entity.UserInfo;
import com.cc.mapper.UserInfoMapper;
import com.cc.service.IUserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
//业务层 ===基于注解声明式式事务
@Component
public class UserInfoServiceImpl4 implements IUserInfoService {
       @Autowired(required=false)//required=false
        private UserInfoMapper userInfoMapper;
       //内部类
    
    @Transactional
    public boolean zz(int from, int to, int money) {
            return zzOptional(from,to,money);
    }
  
    public boolean zzOptional(int from, int to, int money) throws RuntimeException {
        // 一,==============旺财的钱减少300==============
        // 1.1查询旺财有多少钱
        UserInfo wc = userInfoMapper.selectByPrimaryKey(from);
        System.out.println(wc);
        // 1.2扣旺财有300
        int result = 0;
        if(wc!=null) {
            wc.setMoney(wc.getMoney() - money);
            result = userInfoMapper.updateByPrimaryKey(wc);
        }
        // ==============二,来福的钱增加300==============
        // 1.1查询旺财有多少钱
        UserInfo lf = userInfoMapper.selectByPrimaryKey(to);
        System.out.println(lf);
        // 1.2扣旺财有300
        int result2 = 0;
        if (lf != null) {
            lf.setMoney(lf.getMoney() + money);
            result2 = userInfoMapper.updateByPrimaryKey(lf);
        }
        // ==============最后的结果==============
        if (result > 0 && result2 > 0) {
            System.out.println("转账成功！！");
            return true;
        } else {
            System.out.println("转账失败！！");
            throw new RuntimeException("转账失败");
        }
    }
}






















